package com.diandiansong.app.ui.cart;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.diandiansong.app.R;
import com.diandiansong.app.entity.AddressInfo;
import com.diandiansong.app.entity.AddressInsertInfo;
import com.diandiansong.app.ui.base.BaseAct;
import com.diandiansong.app.utils.A;
import com.diandiansong.app.utils.AddressPickTask;

import butterknife.BindView;
import butterknife.OnClick;
import cn.qqtheme.framework.entity.City;
import cn.qqtheme.framework.entity.County;
import cn.qqtheme.framework.entity.Province;
import diandiansong.library.utils.BaseEntity;
import diandiansong.library.utils.CCallBack;

/**
 * "新增地址" : "修改地址"
 */
public class AddressEdit extends BaseAct {

    @BindView(R.id.et_user)
    EditText mEtUser;
    @BindView(R.id.et_phone)
    EditText mEtPhone;
    @BindView(R.id.tv_city)
    TextView mTvCity;
    @BindView(R.id.et_address)
    EditText mEtAddress;
    @BindView(R.id.cb_cb)
    CheckBox mCbCb;

    public static void startAdd(Context context) {
        mAddress = null;
        Intent starter = new Intent(context, AddressEdit.class);
        context.startActivity(starter);
    }

    public static void startEdit(Context context, AddressInfo.Data address) {
        mAddress = address;
        Intent starter = new Intent(context, AddressEdit.class);
        context.startActivity(starter);
    }

    public static AddressInfo.Data mAddress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initTitle(mAddress == null ? "新增地址" : "修改地址");

        addContentView(R.layout.address_edit);

        if (mAddress == null) {
            mAddress = new AddressInfo.Data();
            mAddress.setProvince("");
            mAddress.setCounty("");
            mAddress.setCitys("");

        } else {
            initRithtText("删除", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    A.addressDel(mAddress.getId(), new CCallBack<BaseEntity>() {
                        @Override
                        public void onSuccess(BaseEntity baseEntity) throws Exception {
                            AddressChoose.SubFragment.doRefreshX();
                            finish();
                        }

                        @Override
                        public void onFailure(int code, String msg) {
                            toast(msg);
                        }
                    });
                }
            });

            setText(mEtAddress, mAddress.getAddr());
            setText(mEtPhone, mAddress.getMobile());
            setText(mEtUser, mAddress.getNames());
            setText(mTvCity, mAddress.getArea());
            mCbCb.setChecked(mAddress.getIsDefault());
        }
    }

    @OnClick({R.id.tv_city, R.id.do_save, R.id.do_default})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_city:
                //地址选择器
                AddressPickTask task = new AddressPickTask(getActivity());
                task.setHideProvince(false);
                task.setHideCounty(false);
                task.setCallback(new AddressPickTask.Callback() {
                    @Override
                    public void onAddressInitFailed() {
                        toast("数据初始化失败");
                    }

                    @Override
                    public void onAddressPicked(Province province, City city, County county) {
                        if (county == null) {
                            //toast(province.getAreaName() + city.getAreaName());
                            mAddress.setProvince(province.getAreaName());
                            mAddress.setProvinceId(province.getAreaId());
                            mAddress.setCitys(city.getAreaName());
                            mAddress.setCitysId(city.getAreaId());
                            mAddress.setCounty("");
                            mAddress.setCountyId("");

                            setText(mTvCity, province.getAreaName() + city.getAreaName());

                        } else {
                            //toast(province.getAreaName() + city.getAreaName() + county.getAreaName());
                            mAddress.setProvince(province.getAreaName());
                            mAddress.setProvinceId(province.getAreaId());
                            mAddress.setCitys(city.getAreaName());
                            mAddress.setCitysId(city.getAreaId());
                            mAddress.setCounty(county.getAreaName());
                            mAddress.setCountyId(county.getAreaId());

                            setText(mTvCity, province.getAreaName() + city.getAreaName() + county.getAreaName());
                        }
                    }
                });
                task.execute(mAddress.getProvince(), mAddress.getCitys(), mAddress.getCounty());
                break;

            case R.id.do_save:
                String user = getText(mEtUser);
                String phone = getText(mEtPhone);
                String addr = getText(mEtAddress);
                if (!checkInput(mEtUser, mEtPhone, mEtAddress)) {
                    return;
                }
                if (mAddress.getId() == null) {
                    A.addressInsert(
                            user,
                            phone,
                            mAddress.getProvinceId(),
                            mAddress.getCitysId(),
                            mAddress.getCountyId(),
                            addr,
                            mCbCb.isChecked(),
                            new CCallBack<AddressInsertInfo>() {
                                @Override
                                public void onSuccess(AddressInsertInfo o) throws Exception {
                                    AddressChoose.SubFragment.doRefreshX();
                                    AddressInsertInfo.Data data = o.getData();
                                    String addr_id = data.getAddr_id();
                                    mAddress.setId(addr_id);
                                    finish();
                                }

                                @Override
                                public void onFailure(int code, String msg) {
                                    toast(msg);
                                }
                            }
                    );

                } else {
                    A.addressSave(
                            mAddress.getId(),
                            user,
                            phone,
                            mAddress.getProvinceId(),
                            mAddress.getCitysId(),
                            mAddress.getCountyId(),
                            addr,
                            mCbCb.isChecked(),
                            new CCallBack<AddressInsertInfo>() {
                                @Override
                                public void onSuccess(AddressInsertInfo o) throws Exception {
                                    AddressChoose.SubFragment.doRefreshX();
                                    AddressInsertInfo.Data data = o.getData();
                                    String addr_id = data.getAddr_id();
                                    mAddress.setId(addr_id);
                                    finish();
                                }

                                @Override
                                public void onFailure(int code, String msg) {
                                    toast(msg);
                                }
                            }
                    );
                }
                break;

            case R.id.do_default:
                mCbCb.setChecked(!mCbCb.isChecked());
                break;
        }
    }
}
